﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using hnrzxt.Models;
using hnrzxt.data;
using System.Security.Cryptography;

namespace hnrzxt.Controllers
{
    public class OrgsController : Controller
    {
        private readonly ApplicationDbContext _context;

        public OrgsController(ApplicationDbContext context)
        {
            _context = context;
        }

        public IActionResult Index()
        {
              return View();
        }

        //GET: Orgs
        public async Task<IActionResult> DataDetails()
        {
            return _context.Orgs != null ?
                        View(await _context.Orgs.ToListAsync()) :
                        Problem("Entity set 'ApplicationDbContext.Org'  is null.");
        }

        //GET: Orgs
        public async Task<IActionResult> UserOrg()
        {
            return _context.Users != null ?
                        View(await _context.Users.ToListAsync()) :
                        Problem("Entity set 'ApplicationDbContext.Org'  is null.");
        }

        // GET: Orgs/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null || _context.Orgs == null)
            {
                return NotFound();
            }

            var org = await _context.Orgs
                .FirstOrDefaultAsync(m => m.id == id);
            if (org == null)
            {
                return NotFound();
            }

            return View(org);
        }

        // GET: Orgs/Create //增加同级
        //public IActionResult Create()
        public async Task<IActionResult> Create(int? id)
        {
            if (id == null || _context.Orgs == null)
            {
                return NotFound();
            }

            var org = await _context.Orgs
                .FirstOrDefaultAsync(m => m.id == id);
            if (org == null)
            {
                return NotFound();
            }

            return View(org);
        }

        // POST: Orgs/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("OrgId,OrgName,ParentOrgId,Deptor,WebUrl,Icon,Remark,Sort,StopFlag")] Org org)
        {
            if (ModelState.IsValid)
            {
                _context.Add(org);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(org);
        }

        // GET: Orgs/Create //增加下级
        //public IActionResult Create()
        public async Task<IActionResult> CreateChild(int? id)
        {
            if (id == null || _context.Orgs == null)
            {
                return NotFound();
            }

            var org = await _context.Orgs
                .FirstOrDefaultAsync(m => m.id == id);
            if (org == null)
            {
                return NotFound();
            }

            return View(org);
        }

        // POST: Orgs/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> CreateChild([Bind("OrgId,OrgName,ParentOrgId,Deptor,WebUrl,Icon,Remark,Sort,StopFlag")] Org org)
        {
            if (ModelState.IsValid)
            {
                _context.Add(org);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(org);
        }

        // GET: Orgs/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null || _context.Orgs == null)
            {
                return NotFound();
            }

            var org = await _context.Orgs.FindAsync(id);
            if (org == null)
            {
                return NotFound();
            }
            return View(org);
        }

        // POST: Orgs/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("id,OrgId,OrgName,ParentOrgId,Deptor,WebUrl,Icon,Remark,Sort,StopFlag")] Org org)
        {
            if (id != org.id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(org);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrgExists(org.id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(org);
        }

        // GET: Orgs/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null || _context.Orgs == null)
            {
                return NotFound();
            }

            var org = await _context.Orgs
                .FirstOrDefaultAsync(m => m.id == id);
            if (org == null)
            {
                return NotFound();
            }

            return View(org);
        }

        // POST: Orgs/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            if (_context.Orgs == null)
            {
                return Problem("Entity set 'ApplicationDbContext.Org'  is null.");
            }
            var org = await _context.Orgs.FindAsync(id);
            if (org != null)
            {
                _context.Orgs.Remove(org);
            }
            
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool OrgExists(int id)
        {
          return (_context.Orgs?.Any(e => e.id == id)).GetValueOrDefault();
        }
    }
}
